import SnowflakeId from "snowflake-id";
const { Op } = require("sequelize");
const UnivModels = require('../models/univ');

interface univ {
    schoolId?: number
    level?: string,
    cityId: number,
    other?: string,
    schoolName: string,
    department?: string
}

class UnivService {
    // 获取高校列表
    public async getUnivs() {
        const res: any = await UnivModels.findAndCountAll()
        return res ? res : null
    }
    // 分页获取高校列表
    public async getUnivsByPage(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UnivModels.findAndCountAll({
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据关键词搜索高校列表
    public async getByname(name: string) {
        // 不分页直接获取
        const { count, rows } = await UnivModels.findAndCountAll({
            where: {
                school_name: {
                    [Op.like]: `%${name}%`,
                }
            }
        })
        return {
            total: count,
            list: rows
        }
    }
    // 分页获取-根据关键词搜索高校列表
    public async getBynameandPage(pageNum: number, pageSize: number, name: string) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UnivModels.findAndCountAll({
            where: {
                school_name: {
                    [Op.like]: `%${name}%`,
                }
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据关键词搜索高校列表
    public async getBycityId(cityId: number) {
        // 不分页直接获取
        const { count, rows } = await UnivModels.findAndCountAll({
            where: {
                city_id: cityId
            }
        })
        return {
            total: count,
            list: rows
        }
    }
    // 分页获取-根据关键词搜索高校列表
    public async getBycityIdandPage(pageNum: number, pageSize: number, cityId: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UnivModels.findAndCountAll({
            where: {
                city_id: cityId
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据id获取高校
    public async getUnivsById(id: number) {
        const res = await UnivModels.findOne({
            where: {
                id
            }
        })
        return res ? res.dataValues : null
    }
    // 添加高校
    public async createUniv(params: univ) {
        const res: any = await UnivModels.create(params)
        return res ? res.dataValues : null;
    }
    // 更新高校
    public async update(id: number, params: univ) {
        const whereOpt = { id }
        const res = await UnivModels.update(params, { where: whereOpt })
        return res[0] > 0
    }
    // 删除高校
    public async delUniv(id: number) {
        const res: any = await UnivModels.destroy({
            where: { id }
        })
        return res > 0
    }
}

module.exports = new UnivService()
// 解决同目录下多个ts文件都引入Op,会报错的问题
export { }